<?php

namespace App\Http\Controllers\Admin;

use App\Common\Controllers\Admin\AdminController;
use App\Models\PromotionConvertModel;
use App\Services\PromotionConvertService;
use Illuminate\Http\Request;

class PromotionConvertController extends AdminController
{
    /**
     * @var string
     * 默认排序字段
     */
    protected $defaultOrderBy = 'cost';

    /**
     * constructor.
     */
    public function __construct()
    {
        $this->model = new PromotionConvertModel();

        parent::__construct();
    }

    /**
     * 列表预处理
     */
    public function selectPrepare(){
        $this->service = new PromotionConvertService();
        $this->totalMap = [];

        $this->curdService->selectQueryAfter(function(){
            foreach($this->curdService->responseData['list'] as $k => $v){
//                $v->ocean_project;
//                $v->ocean_promotion;
//
//                $this->curdService->responseData['list'][$k]['project_name'] = $v->ocean_project['name'] ?? '';
//                $this->curdService->responseData['list'][$k]['promotion_name'] = $v->ocean_promotion['name'] ?? '';
//
//                unset($v->ocean_project);
//                unset($v->ocean_promotion);


                if(!empty($this->curdService->requestData['is_total'])){
                    // 总数据
                    if(!isset($this->totalMap[$v['promotion_id']])){
                        $stat = $this->service->getPromotionConvertStat($v['promotion_id']);

                        $stat['bid'] = $stat['bid'] / 100;
                        $stat['cost'] = $stat['cost'] / 100;

                        $stat['convert_cost'] = $stat['convert_callback_count'] > 0 ? round($stat['cost'] / $stat['convert_callback_count'], 2) : 0;
                        $totalConvertCallbackCount = $stat['convert_callback_count'] + $stat['no_convert_callback_count'];
                        $stat['real_convert_cost'] = $totalConvertCallbackCount > 0 ? round($stat['cost'] / $totalConvertCallbackCount, 2) : 0;
                        $this->totalMap[$v['promotion_id']] = $stat;
                    }else{
                        $stat = $this->totalMap[$v['promotion_id']];
                    }

                    $this->curdService->responseData['list'][$k]['bid'] = $stat['bid'];
                    $this->curdService->responseData['list'][$k]['cost'] = $stat['cost'];
                    $this->curdService->responseData['list'][$k]['convert_callback_count'] = $stat['convert_callback_count'];
                    $this->curdService->responseData['list'][$k]['no_convert_callback_count'] = $stat['no_convert_callback_count'];
                    $this->curdService->responseData['list'][$k]['convert_cost'] = $stat['convert_cost'];
                    $this->curdService->responseData['list'][$k]['real_convert_cost'] = $stat['real_convert_cost'];
                }else{
                    // 当天数据
                    $this->curdService->responseData['list'][$k]['bid'] = $v['bid'] / 100;
                    $this->curdService->responseData['list'][$k]['cost'] = $v['cost'] / 100;
                    $this->curdService->responseData['list'][$k]['convert_cost'] = $v['convert_cost'] / 100;
                    $this->curdService->responseData['list'][$k]['real_convert_cost'] = $v['real_convert_cost'] / 100;
                }
            }
        });


    }
}
